package com.fastwire.carriere.service;

import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.controller.NetworkElementController;
import com.fastwire.carriere.controller.impl.NetworkElementControllerImpl;
import com.fastwire.carriere.inventory.vo.NetworkElementVo;
import com.fastwire.carriere.service.dataconverter.XMLConverter;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.user.vo.SessionVo;

// TODO: Auto-generated Javadoc
/**
 * The Class NEInventoryServiceImpl.
 */
@WebService(endpointInterface = "com.fastwire.carriere.service.NEInventoryService")
public class NEInventoryServiceImpl implements NEInventoryService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.NEInventoryService#getAllNETypes()
	 */
	@Override
	public List<String> getAllNETypes(SessionVo sessionVo)
			throws ServiceException {

		try {
			NetworkElementController neController = (NetworkElementController) ResourceUtil
					.getSessionBindClassByName(
							NetworkElementControllerImpl.class.getName(),
							sessionVo, true);
			List<String> neTypes = neController.getDistinctNETypes();
			return neTypes;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.NEInventoryService#getNEbySearchCriteria
	 * (com.fastwire.carriere.inventory.vo.NetworkElementVo)
	 */
	@Override
	public List<String> getNEbySearchCriteria(NetworkElementVo neVo,
			SessionVo sessionVo) throws ServiceException {

		try {

			NetworkElementController neController = (NetworkElementController) ResourceUtil
					.getSessionBindClassByName(
							NetworkElementControllerImpl.class.getName(),
							sessionVo, true);

			List<String> neVos = neController.searchNE(neVo);

			return neVos;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.NEInventoryService#getNEBayFacebyNeId(java
	 * .lang.String)
	 */
	@Override
	public String getNEBayFacebyNeId(String neId, SessionVo sessionVo)
			throws ServiceException {

		try {

			NetworkElementController neController = (NetworkElementController) ResourceUtil
					.getSessionBindClassByName(
							NetworkElementControllerImpl.class.getName(),
							sessionVo, true);

			NetworkElementVo neVo = neController.getNEBayFacebyNeId(neId);
			
			return XMLConverter.serilizeObjectToXML(neVo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	// added by Jun on 20120625
	@Override
	public String getRadBayFacebyIP(String neId, SessionVo sessionVo) throws ServiceException
	{
        try
        {
            NetworkElementController neController = (NetworkElementController) ResourceUtil
                    .getSessionBindClassByName(NetworkElementControllerImpl.class.getName(),
                            sessionVo, true);

            //NetworkElementVo neVo = neController.getRadBayFaceByIP("129.9.0.41");
            NetworkElementVo neVo = neController.getRadBayFaceByIP(neId);

            return XMLConverter.serilizeObjectToXML(neVo);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();

            String ex = ControllerProxy.getRootException(e);
            throw new ServiceException(ex);
        }
    }

	
	@Override
	public String updateNE(NetworkElementVo neVo, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		if (StringUtil.isEmptyString(neVo.getRemarks())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "Remark" });
		}

		if (StringUtil.isEmptyString(neVo.getNativeEmsName())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "NE Native EMS Name" });
		}

		try {

			NetworkElementController neController = (NetworkElementController) ResourceUtil
					.getSessionBindClassByName(
							NetworkElementControllerImpl.class.getName(),
							sessionVo, true);
			neController.updateNE(neVo);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

		return msg;

	}

}
